package com.example.movie.mate.service.impl;

import com.example.movie.mate.dao.mapper.HallMapper;
import com.example.movie.mate.dao.mapper.SeatMapper;
import com.example.movie.mate.exception.ServiceException;
import com.example.movie.mate.pojo.entity.Hall;
import com.example.movie.mate.pojo.entity.Seat;
import com.example.movie.mate.pojo.param.HallAddNewParam;
import com.example.movie.mate.pojo.vo.HallVO;
import com.example.movie.mate.service.IHallService;
import com.example.movie.mate.web.StateCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class HallServiceImpl implements IHallService {

    @Autowired
    private HallMapper hallMapper;
    @Autowired
    private SeatMapper seatMapper;

    @Override
    public void addNew(HallAddNewParam param) {
        Hall hall = new Hall();
        BeanUtils.copyProperties(param, hall);
        hallMapper.insert(hall);

        Integer rowCount = param.getRowCount();
        Integer colCount = param.getColCount();
        List<Seat> seatList = new ArrayList<>();
        for (int i = 1; i <= rowCount; i++) {
            for (int j = 1; j <= colCount; j++) {
                Seat seat = new Seat();
                seat.setHallId(hall.getId());
                seat.setRowNum(i);
                seat.setColNum(j);
                seatList.add(seat);
            }
        }
        seatMapper.insert(seatList);
    }

    @Override
    public HallVO selectById(Long id) {
        HallVO findResult = hallMapper.selectById(id);
        if (findResult == null) {
            throw new ServiceException(StateCode.ERROR_NOT_FOUND, "影厅不存在");
        }
        return findResult;
    }

    @Override
    public List<HallVO> selectAll() {
        return hallMapper.selectAll();
    }
}